home *** CD-ROM | disk | FTP | other *** search
/ The Business Master (3rd Edition) / The Business Master (3rd Edition).iso / files / business / stata2 / qc.kit < prev    next >
Encoding:
Text File  |  1988-08-29  |  6.4 KB  |  234 lines

  1. set output error
  2.  
  3. display _new in blue /*
  4. */ "Loading QC.Kit Release 2.0  Demonstration Version" _n /*
  5. */ "Copyright (c) 1986-1988 by ==C=R=C==.  All rights reserved."
  6.  
  7. /*
  8.   User's wishing to edit QC.Kit please see comments at end of this file.
  9. */
  10.  
  11.  
  12. /*
  13.     cchart defects unit, graph_options
  14. */
  15.  
  16. capture program drop cchart
  17. program define cchart
  18.     if "%_*"=="" {
  19.         #delimit ;
  20.         di in bl "-> cchart defects unit [, graph_options]" _n
  21.         in ye _col(11) "defects " in gr
  22.         "- the number of defects in each inspection unit" _n  in ye
  23.         _col(14) "unit " in gr
  24.         "- the inspection unit number (need not be in order)";
  25.         #delimit cr
  26.         exit
  27.     }
  28.     mac def _varlist "req ex min(2) max(2)"
  29.     mac def _options "*"
  30.     parse "%_*"
  31.     parse "%_varlist", parse(" ")
  32.     mac def _DEFECTS "%_1"
  33.     mac def _UNIT "%_2"
  34.     capture drop _NUNITS
  35.     capture drop _CBAR
  36.         /*
  37.            calculate _CBAR, the mean number of defects
  38.         */
  39.     gen int _NUNITS = sum(%_DEFECTS~=. & %_DEFECTS>=0 & %_UNIT~=.)
  40.     gen float _CBAR = sum(%_DEFECTS)
  41.     mac def _CBAR = _CBAR[_N]/_NUNITS[_N]
  42.     drop _CBAR
  43.         /*
  44.            calculate the control limits and draw the c-chart
  45.         */
  46.     mac def _UCL = %_CBAR + 3*sqrt(%_CBAR)
  47.     mac def _LCL = cond(%_CBAR>3*sqrt(%_CBAR),%_CBAR-3*sqrt(%_CBAR),%_UCL)
  48.     quietly replace _NUNITS = sum(%_DEFECTS<%_LCL | %_DEFECTS>%_UCL)
  49.     mac def _top "(1 unit is out of control)"
  50.     if _NUNITS[_N]!=1 {
  51.         mac def _top = _NUNITS[_N]
  52.         mac def _top = "(%_top units are out of control)"
  53.     }
  54.     #delimit ;
  55.     gr %_DEFECTS %_UNIT, c(l) s(o) sort %_options
  56.         rlab(%_CBAR,%_UCL,%_LCL) yline(%_UCL,%_LCL)
  57.         t1("%_top") l1("Number of defects") ;
  58.     #delimit cr
  59.     drop _NUNITS
  60. end
  61.  
  62.  
  63. capture program drop pchart
  64. program define pchart
  65.     if "%_*"=="" {
  66.         #delimit ;
  67.         di in bl
  68.         "-> pchart rejects unit smplsize [, stabilized graph_options]"
  69.         _n in ye
  70.         _col(11) "rejects" in gr
  71.         " - the number rejected in each inspection unit" _n in ye
  72.         _col(14) "unit" in gr
  73.         " - the inspection unit number (need not be in order)" _n in ye
  74.         _col(10) "smplsize" in gr " - the number in each inspection unit" ;
  75.         #delimit cr
  76.         exit
  77.     }
  78.     mac def _varlist "req ex min(3) max(3)"
  79.     mac def _options "STAbilized *"
  80.     parse "%_*"
  81.     parse "%_varlist", parse(" ")
  82.     mac def _rejects "%_1"
  83.     mac def _unit "%_2"
  84.     mac def _sample "%_3"
  85.     capture drop _ntotal
  86.     capture drop _p
  87.     capture drop _pbar
  88.         /*
  89.            calculate _p, the fraction rejected
  90.         */
  91.     gen _temp = -%_sample
  92.     sort _temp
  93.     drop _temp
  94.     gen int _ntotal = sum(max(0,(%_sample)))
  95.     if _ntotal[_N] <= 0 {
  96.         di in red "All samples are missing or zero"
  97.         drop _ntotal
  98.         exit
  99.     }
  100.     gen float _p = cond(%_rejects/%_sample>=0,%_rejects/%_sample,.)
  101.     label variable _p "Fraction defective"
  102.     gen float _pbar = sum(max(0,%_rejects))
  103.     mac def _pbar = _pbar[_N]/_ntotal[_N]
  104.     drop _pbar
  105.     if %_pbar <= 0 {
  106.         di in red "No units were ever rejected"
  107.         drop _ntotal _p
  108.         exit
  109.     }
  110.         /*
  111.            calculate the control limits
  112.         */
  113.     quietly count if %_sample>0 & %_sample!=. & %_sample!=%_sample[1]
  114.     if _result(1)==0 {      /* constant sample size */
  115.         mac def _ucl=3*sqrt(%_pbar*(1-%_pbar)/(%_sample)) + %_pbar
  116.         mac def _lcl=cond(%_pbar>%_ucl-%_pbar,2*%_pbar-%_ucl,%_ucl)
  117.         quietly replace _ntotal = sum(_p<%_lcl | _p>%_ucl)
  118.         mac def _t2=_ntotal[_N]
  119.         mac def _t2="%_t2 units are out of control"
  120.         format _p %9.4f
  121.         #delimit ;
  122.         gr _p %_unit, c(l) s(o) sort %_options
  123.             t1("%_t2") yline(%_ucl,%_lcl)
  124.             rlab(%_ucl,%_lcl,%_pbar)
  125.             l1("Fraction defective") ;
  126.         #delimit cr
  127.         drop _ntotal _p
  128.         exit
  129.     }
  130.             /* varying sample size  */
  131.     gen float _ucl = 3*sqrt(%_pbar*(1-%_pbar)/(%_sample))
  132.     gen float _lcl = cond(%_pbar>_ucl,%_pbar-_ucl,.)
  133.     quietly replace _ucl = %_pbar + _ucl
  134.     label variable _ucl " "
  135.     label variable _lcl " "
  136.  
  137.     quietly replace _ntotal = sum(_p<_lcl | _p>_ucl)
  138.     mac def _t2=_ntotal[_N]
  139.     mac def _t2 "%_t2 units are out of control"
  140.     if "%_stabili"=="" {
  141.         format _lcl %9.4f
  142.         #delimit ;
  143.         gr _lcl _ucl _p %_unit, c(lll) s(iio) sort %_options pen(112)
  144.             rlab(%_pbar) t1("%_t2") l1("Fraction defective") ;
  145.         #delimit cr
  146.         drop _ntotal _p _lcl _ucl
  147.         exit
  148.     }
  149.     quietly replace _ntotal = sum(_ucl~=_ucl[1])
  150.     quietly replace _ucl = (_ucl-%_pbar)/3
  151.     quietly replace _p = (_p-%_pbar)/_ucl
  152.     format _p %9.2f
  153.     #delimit ;
  154.     gr _p %_unit, c(l) s(o) sort
  155.         yline(-3,3) %_options
  156.         rlabel(-3,0,3)
  157.         t1("Stabilized p-Chart, average number of defects = %_pbar")
  158.         t2("%_t2")
  159.         l1("Fraction defective") l2("(Standard Deviation units)") ;
  160.     #delimit cr
  161.     drop _ntotal _p _lcl _ucl
  162. end
  163.  
  164.  
  165.  
  166. capture program drop plotebar
  167. program define plotebar
  168.     if "%_*"=="" {
  169.         #delimit ;
  170.         di in bl
  171.         "-> plotebar mean std time [, scale(#) graph_options]" _n
  172.         _col(13) in ye "mean"
  173.         in gr " - process mean" _n
  174.         _col(14) in ye "std"
  175.         in gr " - process standard deviation" _n
  176.         _col(13) in ye "time"
  177.         in gr " - time variable" _n
  178.         _col(16) in ye "# "
  179.         in gr "- number of standard deviations, default is 1" ;
  180.         #delimit cr
  181.         exit
  182.     }
  183.     mac def _varlist "req ex min(3) max(3)"
  184.     mac def _options "SCale(int 1) *"
  185.     parse "%_*"
  186.     parse "%_varlist", parse(" ")
  187.     mac def _mean "%_1"
  188.     mac def _std "%_2"
  189.     mac def _time "%_3"
  190.             /*
  191.                For non-demonstration version, we should save the
  192.                data at this point
  193.             */
  194.     quietly {
  195.         drop if %_mean==. | %_std==. | %_time==.
  196.         keep %_mean %_std %_time
  197.         expand =4, clear
  198.         sort %_time
  199.         gen int _type=mod(_n,4)
  200.         gen double _result=%_mean if _type<=1
  201.         replace _result=%_mean+%_scale*%_std if _type==2
  202.         replace _result=%_mean-%_scale*%_std if _type==3
  203.     }
  204.     gr %_mean _result %_time, c(.l) sy(Oi) %_options
  205.             /*
  206.                For non-demonstration version, we should now
  207.                restore the data
  208.             */
  209. end
  210.  
  211.  
  212. di
  213. di in gr "The following new commands are now available:"
  214. di "   cchart         pchart         plotebar"
  215. exit
  216.  
  217.  
  218. Notes to User's wishing to edit this file:
  219.  
  220. These comments are placed at the end of the file, after the exit, so that
  221. Stata does not have to read through them every time the user types "run
  222. QC.Kit"
  223.  
  224. Feel free to edit this file and delete programs in which you have no interest.
  225. Before editing this file, however, make sure the the original is BACKED UP.
  226. Please do not remove the copyright notice at the top of this file.
  227.  
  228. If you do edit the file, you should add a display command at the top of this
  229. file indicating that you have edited the file.  You should change the display
  230. just above these comments that lists the new commands now available.
  231.  
  232.  
  233. <end of QC.Kit>
  234.